package com.bckj.fastboot.core.sql.meta;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.bckj.fastboot.core.lang.func.IGetter;
import com.bckj.fastboot.core.util.LambdaMeta;
import com.bckj.fastboot.core.util.LambdaUtils;

import java.util.Collection;

public interface TableLike {

    String getTableName();

    Collection<Column> getColumns();

    default String getAlias() {
        return null;
    }

    default <T> Column column(IGetter<T> getter) {
        return column(getter, null);
    }

    default <T> Column column(IGetter<T> getter, String alias) {
        LambdaMeta lambdaMeta = LambdaUtils.getSerializedLambda(getter);
        String fieldName = lambdaMeta.getFieldName();
        String colName = StringUtils.camelToUnderline(fieldName);
        return column(colName, alias);
    }

    default Column column(String column, String alias) {
        Column col = new Column(this, column, alias);
        getColumns().add(col);
        return col;
    }

    default Column column(String column) {
        return column(column, null);
    }
}
